package product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import product.domain.ProductAttrValue;
import product.service.ProductAttrValueService;
import product.mapper.ProductAttrValueMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author panghu
 * @description 针对表【pms_product_attr_value(spu属性值)】的数据库操作Service实现
 * @createDate 2023-03-04 21:03:08
 */
@Service
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueMapper, ProductAttrValue>
        implements ProductAttrValueService {

    @Override
    public List<ProductAttrValue> baseAttrValueListForSpu(Long spuId) {
        return this.baseMapper.selectList(new LambdaQueryWrapper<ProductAttrValue>().eq(ProductAttrValue::getSpuId, spuId));
    }

    @Override
    @Transactional
    public void updateAttrBySkuId(Long spuId, List<ProductAttrValue> attrValues) {
        // 删除原来的数据
        this.remove(new LambdaQueryWrapper<ProductAttrValue>().eq(ProductAttrValue::getSpuId, spuId));
        // 新增修改后的数据
        List<ProductAttrValue> collect = attrValues.stream().peek(item -> {
            item.setSpuId(spuId);
        }).collect(Collectors.toList());
        this.saveBatch(collect);
    }
}




